const app = getApp();
const md5 = require('../../assets/js/md5/md5.js');
Page({
 //页面的初始数据
 data: {
  username: '',
  phone: '',
  passwordold: '',
  passwordnew: '',
  passwordnew2: ''
 },

//  bindinputusername: function(e) {
//   this.setData({
//    username: e.detail.value
//   })
//  },

 bindinputphone: function(e) {
  this.setData({
   phone: e.detail.value
  })
 },
 bindinputpasswordold: function(e) {
  this.setData({
   passwordold: e.detail.value
  })
 },
 bindinputpasswordnew: function(e) {
  this.setData({
   passwordnew: e.detail.value
  })
 },
 bindinputpasswordnew2: function(e) {
  this.setData({
    passwordnew2: e.detail.value
  })
 },


 //修改个人信息
 formSubmit: function() {
  var that = this;
  // let username = that.data.username;
  let phone = that.data.phone;
  let passwordold = that.data.passwordold;
  let passwordnew = that.data.passwordnew;
  let passwordnew2 = that.data.passwordnew2;

  // if (username === '') {
  //  wx.showToast({
  //   title: '用户名不能为空',
  //   icon: 'none'
  //  })
  //  return;
  // }
  if (phone === '') {
   wx.showToast({
    title: '手机号不能为空！',
    icon: 'none'
   })
   return;
  }
  if (passwordold === '') {
    wx.showToast({
     title: '老密码不能为空！',
     icon: 'none'
    })
    return;
   }
  if (passwordnew === '') {
    wx.showToast({
     title: '新密码不能为空！',
     icon: 'none'
    })
    return;
   }
   if (passwordnew2 === '') {
    wx.showToast({
     title: '新密码不能为空！',
     icon: 'none'
    })
    return;
   }
   // 两次密码是否相同比对
   if (passwordnew != passwordnew2) {
    wx.showToast({
      title: '两次输入新密码不同！',
      icon: 'none'
     })
     return;
   }
   // 开始密码加密处理
   var passwordold_b64 = md5.hexMD5(phone+passwordold);
   var passwordnew_b64 = md5.hexMD5(phone+passwordnew); 

  wx.request({
   url: app.globalData.baseUrl + '/wechatprogram/WxUser/edit',
   method: "POST",
   header: {
    "Content-Type": "application/x-www-form-urlencoded"
   },
   data: {
    phone: phone,
    passwordold: passwordold_b64,
    passwordnew: passwordnew_b64
   },
   success(res) {
    // 这里修改成跳转的页面
    if (res.data.code == 0) {
      // 重新设置用户信息
      wx.setStorageSync('wx_user',  res.data.data);
      wx.showToast({
        title: '修改成功！',
        icon: 'success',
        duration: 1000,
        success:function(){
          setTimeout(function () {
            //要延时执行的代码
            wx.reLaunch({
              url: '../me/me',
            })
          },1000) //延迟时间
         
        }
      })   
    } else {
      wx.showToast({   
        title: res.data.msg,   
        icon: 'none',   
        duration: 2000   
      }) 
    } 
  },
  fail: (res) => {
    wx.showToast({
      title: '修改失败！',   
      icon: 'none',   
      duration: 2000
    })
  },
  })

 },

 //生命周期函数--监听页面加载
 onLoad: function(options) {
  let that = this;
  var wx_user =  wx.getStorageSync('wx_user');
  if (wx_user == null || wx_user == '') {
    wx.showToast({
      title: '登录信息过期，请登录！',
      icon: 'none',
      duration: 1000,
      success:function(){
        setTimeout(function () {
          //要延时执行的代码,延迟1s
          wx.navigateTo({
            url: 'url',
          })({
            url: '../login/login',
          })
        },1000)
      }
    })   
  } else {
    // 用于登录用户手机号码回填
    that.setData({
      phone: wx_user.iphone
     })
  }
 },
})